package com.singfan.core.dao.database.impl;

import com.singfan.common.db.DaoBaseGeneric;
import com.singfan.common.db.LongResultSetReader;
import com.singfan.core.dao.database.IBarberAppointmentDao;
import com.singfan.core.dao.database.model.BarberAppointment;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Author: hansonghe
 * Date: 2015-08-19 12:12
 * Desc:
 */
@Repository
public class BarberAppointmentDaoImpl extends DaoBaseGeneric<BarberAppointment> implements IBarberAppointmentDao {
    @Override
    protected String getTableName(Object hashKey) {
        return tableNameBase;
    }

    @Override
    protected String getDatabaseId(Object hashKey) {
        return "app_1";
    }

    @Override
    public void create(BarberAppointment barberAppointment) {
        insertWithAutoIncrement(barberAppointment.getBarberId(), barberAppointment);
    }

    @Override
    public List<BarberAppointment> selectAppointmentListByDayTime(long barberId, int dayTime) {
        String sql = "SELECT * FROM " + getTableName(barberId) + " WHERE barber_id=? AND appointment_day=?";

        return selectList(barberId, sql, barberId, dayTime);
    }

    @Override
    synchronized public BarberAppointment selectAppointmentByDayClockTime(long barberId, int dayTime, int clockTime) {
        String sql = "SELECT * FROM " + getTableName(barberId) + " WHERE barber_id=? AND appointment_day=? AND appointment_clock=?";

        return selectOne(barberId, sql, barberId, dayTime, clockTime);
    }

    @Override
    public void deleteAppointment(long barberId, int appointmentDay, int appointmentClock) {
        String sql = "DELETE FROM " + getTableName(barberId) + " WHERE barber_id=? AND appointment_day=? AND appointment_clock=?";
        execute(null, sql, barberId, appointmentDay, appointmentClock);
    }

    @Override
    public void deleteAppointment(long orderId) {
        String sql = "DELETE FROM " + getTableName(null) + " WHERE order_id=?";
        execute(null, sql, orderId);
    }

    @Override
    public List<Long> barberHasAppointmentByDayClockTime(List<Long> barberIdList, int dayTime, int clockTime) {
        List<Long> result = new ArrayList<>();
        if (barberIdList != null){
            int barberIdCount = barberIdList.size();
            List<Object> paramList = new ArrayList<>();
            paramList.add(dayTime);
            paramList.add(clockTime);
            if (barberIdCount > 0){
                String sql = "SELECT barber_id FROM " + getTableName(null) + " WHERE appointment_day=? AND appointment_clock=? AND barber_id in (";
                for (int i=0; i<barberIdCount; i++){
                    if (i == 0){
                        sql += "?";
                    }
                    else {
                        sql += ",?";
                    }

                    paramList.add(barberIdList.get(i));
                }

                sql += ")";

                result = (List<Long>)executeQuery(null, sql, paramList, new LongResultSetReader());
            }
        }
        return result;
    }
}
